<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet
   version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fn="http://www.w3.org/2005/xpath-functions"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
   xmlns:owl="http://www.w3.org/2002/07/owl#"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:dcterms="http://purl.org/dc/terms/"
   xmlns:dcmiype="http://purl.org/dc/dcmitype/"
   xmlns:bibo="http://purl.org/ontology/bibo/"
   xmlns:foaf="http://xmlns.com/foaf/0.1/"
   xmlns:bio="http://purl.org/vocab/bio/0.1/"
   xmlns:skos="http://www.w3.org/2004/02/skos/core#"
   xmlns:wgs84_pos="http://www.w3.org/2003/01/geo/wgs84_pos#"
   xmlns:ore="http://www.openarchives.org/ore/terms/"
   xmlns:lvont="http://lexvo.org/ontology#"
   xmlns:lode = "http://linkedevents.org/ontology/"
   xmlns:event="http://purl.org/NET/c4dm/event.owl#"
   xmlns:time = "http://www.w3.org/2006/time#"
   xmlns:timeline="http://purl.org/NET/c4dm/timeline.owl#"
   xmlns:arch="http://purl.org/archival/vocab/arch#"
   xmlns:crm="http://erlangen-crm.org/current/"
   xmlns:locah="http://data.archiveshub.ac.uk/def/"

>

<!--
Created by Pete Johnston, Eduserv for the SALDA project, 2011

Adapted from the XSLT http://data.archiveshub.ac.uk/xslt/20110502/ead2rdf.xsl
created by Pete Johnston, Eduserv for the LOCAH project, 2010-2011

Copyright (c) 2011, Eduserv
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Eduserv nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL EDUSERV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<!--

moa-ead2rdf.xsl

Extends lookup-ead2rdf.xsl to perform MOA-specific processing.

Requires three parameters:
    root = "base" URI for generating "thing URIs" 
    e.g. root=http://example.org 
    results in URIs like http://example.org/id/archivalresource/gb123abc
    
    authnames = URI of personal names list (XML)
    
    keywords = URI of keywords list (XML)

-->

<xsl:import href="lookup-ead2rdf.xsl" />

<xsl:template match="document-node()">
        <xsl:message>Transforming document: <xsl:value-of select="document-uri(.)" /></xsl:message>
	<xsl:comment>Input document: <xsl:value-of select="document-uri(.)" /></xsl:comment>
	<xsl:comment>XSLT transform: <xsl:value-of select="static-base-uri()" /></xsl:comment>
	<xsl:comment>Time of transform: <xsl:value-of select="current-dateTime()" /></xsl:comment>
	<xsl:comment>Authnames: <xsl:value-of select="$authnames" /></xsl:comment>
	<xsl:comment>Keywords: <xsl:value-of select="$keywords" /></xsl:comment>

	<xsl:apply-templates />
</xsl:template>


<xsl:template name="extend-uod-desc">
<xsl:param name="thing" />

	<xsl:choose>
		<xsl:when test="starts-with(did/unitid[@label='Reference'], 'SxMOA1/1/')">
			<xsl:call-template name="process-file-reports">
				<xsl:with-param name="thing" select="$thing" />
				<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="starts-with(did/unitid[@label='Reference'], 'SxMOA1/2/')">
			<xsl:call-template name="process-topic">
				<xsl:with-param name="thing" select="$thing" />
				<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="starts-with(did/unitid[@label='Reference'], 'SxMOA1/3/')">
			<xsl:call-template name="process-directives">
				<xsl:with-param name="thing" select="$thing" />
				<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="starts-with(did/unitid[@label='Reference'], 'SxMOA1/4/')">
			<xsl:call-template name="process-diaries" >
				<xsl:with-param name="thing" select="$thing" />
				<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="starts-with(did/unitid[@label='Reference'], 'SxMOA1/5/')">
			<xsl:call-template name="process-worktowns" >
				<xsl:with-param name="thing" select="$thing" />
				<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise />
	</xsl:choose>

	<xsl:call-template name="match-personal-name">
		<xsl:with-param name="thing" select="$thing" />
		<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
	</xsl:call-template>
		
	<xsl:call-template name="match-concept-keyword">
		<xsl:with-param name="thing" select="$thing" />
		<xsl:with-param name="label" select="normalize-space(did/unittitle)" />
	</xsl:call-template>

	
</xsl:template>


<!-- SALDA MOA-specific templates -->

<xsl:template name="process-file-reports">
<xsl:param name="thing" />
<xsl:param name="label" />

	<xsl:variable name="dates">
		<xsl:choose>
			<xsl:when test="@level='series' and matches(did/unitid[@label='Reference'], '^SxMOA1/1/[0-9]*$')">
				
				<xsl:value-of select="normalize-space(did/unittitle)" />
				
			</xsl:when>
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/1/[0-9]*$')">
				
				<xsl:value-of select="concat(normalize-space(did/unittitle), ' ',normalize-space(../did/unittitle) )" />
	
			</xsl:when>
			<xsl:when test="matches(../../did/unitid[@label='Reference'], '^SxMOA1/1/[0-9]*$')">
				
				<xsl:value-of select="concat(normalize-space(../did/unittitle), ' ',normalize-space(../../did/unittitle) )" />
	
			</xsl:when>
			<xsl:otherwise />
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="not($dates = '')">
		<xsl:comment>SALDA Date = <xsl:value-of select="$dates" /></xsl:comment>
		
		<xsl:call-template name="make-salda-unitdate">
			<xsl:with-param name="thing" select="$thing"/>
			<xsl:with-param name="label" select="$label"/>
			<xsl:with-param name="dates" select="$dates"/>
		</xsl:call-template>
		
		
	</xsl:if>
	
</xsl:template>

<xsl:template name="process-topic">
<xsl:param name="thing" />
<xsl:param name="label" />

	<xsl:variable name="topicLabel">
		<xsl:choose>
			<xsl:when test="matches(did/unitid[@label='Reference'], '^SxMOA1/2/[0-9]*$')">
				
				<xsl:value-of select="replace(normalize-space(did/unittitle), '^(TC[0-9]*)\s(.*)\s(19\d{2})((\s)?-(\s)?(\d{2}(\d{2})?))?$', '$2')" />
	
				
			</xsl:when>
			<!--
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/2/[0-9]*$')">
				
				<xsl:value-of select="replace(normalize-space(../did/unittitle), '^(TC[0-9]*)\s(.*)\s(19\d{2})((\s)?-(\s)?(\d{2}(\d{2})?))?$', '$2')" />
				
			</xsl:when>
			<xsl:when test="matches(../../did/unitid[@label='Reference'], '^SxMOA1/2/[0-9]*$')">
				
				<xsl:value-of select="replace(normalize-space(../../did/unittitle), '^(TC[0-9]*)\s(.*)\s(19\d{2})((\s)?-(\s)?(\d{2}(\d{2})?))?$', '$2')" />
				
			</xsl:when>
			<xsl:when test="matches(../../../did/unitid[@label='Reference'], '^SxMOA1/2/[0-9]*$')">
				
				<xsl:value-of select="replace(normalize-space(../../../did/unittitle), '^(TC[0-9]*)\s(.*)\s(19\d{2})((\s)?-(\s)?(\d{2}(\d{2})?))?$', '$2')" />
				
			</xsl:when>
			-->
			<xsl:otherwise />
		</xsl:choose>
	</xsl:variable>
	
	<xsl:if test="not($topicLabel = '')">

		<xsl:comment>SALDA Topic = <xsl:value-of select="$topicLabel" /></xsl:comment>
		<xsl:variable name="slug">
			<xsl:call-template name="slugify">
				<xsl:with-param name="text" select="$topicLabel" />
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:variable name="topic" select="concat($id, 'concept/moatopic/', $slug)" />

		<xsl:element name="dcterms:subject">
			<xsl:element name="skos:Concept">
				<xsl:attribute name="rdf:about" select="$topic" />
	
				<xsl:element name="rdfs:label"><xsl:attribute name="xml:lang" select="'en'" /><xsl:value-of select="$topicLabel"/></xsl:element>
				<xsl:element name="skos:prefLabel"><xsl:attribute name="xml:lang" select="'en'" /><xsl:value-of select="$topicLabel"/></xsl:element> 
	
				<xsl:element name="skos:inScheme">
					<xsl:element name="skos:ConceptScheme">
						<xsl:attribute name="rdf:about"><xsl:value-of select="concat($id, 'conceptscheme/moatopic')"/></xsl:attribute>
						<xsl:element name="rdfs:label"><xsl:attribute name="xml:lang" select="'en'" /><xsl:value-of select="'Mass Observation Archive Topics'"/></xsl:element>
					</xsl:element>
				</xsl:element>
			</xsl:element>
		</xsl:element>
		
	</xsl:if>
	
	<xsl:variable name="placeLabel">
		<xsl:choose>
			<xsl:when test="matches(did/unitid[@label='Reference'], '^SxMOA1/2/23/8/[B-Z]*$')">
				
				<xsl:value-of select="normalize-space(did/unittitle)" />
	
			</xsl:when>
			<!-- 
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/2/23/8/[B-Z]*$')">
				
				<xsl:value-of select="normalize-space(../did/unittitle)" />
	
			</xsl:when>
			-->
			
			<xsl:when test="matches(did/unitid[@label='Reference'], '^SxMOA1/2/23/9/[A-Z]*$')">
				
				<xsl:value-of select="normalize-space(did/unittitle)" />
				
			</xsl:when>
			<!--
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/2/23/9/[A-Z]*$')">
				
				<xsl:value-of select="normalize-space(../did/unittitle)" />
	
			</xsl:when>
			-->

			<xsl:when test="matches(did/unitid[@label='Reference'], '^SxMOA1/2/23/10/[A-Z]*$')">
				
				<xsl:value-of select="normalize-space(did/unittitle)" />
	
			</xsl:when>
			<!--
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/2/23/10/[A-Z]*$')">
				
				<xsl:value-of select="normalize-space(../did/unittitle)" />
	
			</xsl:when>
			-->
			
			<xsl:otherwise />
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="not($placeLabel = '')">
	
		<xsl:comment>SALDA Place = <xsl:value-of select="$placeLabel" /></xsl:comment>
		<xsl:variable name="slug">
			<xsl:call-template name="slugify">
				<xsl:with-param name="text" select="$placeLabel" />
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="place" select="concat($id, 'place/', $slug)" />

		<xsl:element name="dcterms:spatial">
			<xsl:element name="wgs84_pos:SpatialThing">	
				<xsl:attribute name="rdf:about"><xsl:value-of select="$place" /></xsl:attribute>
				<xsl:element name="rdfs:label"><xsl:attribute name="xml:lang" select="'en'" /><xsl:value-of select="$placeLabel"/></xsl:element>
			</xsl:element>
		</xsl:element>
		
		<xsl:element name="dcterms:subject">
			<xsl:attribute name="rdf:resource" select="$place" />
		</xsl:element>
	
	</xsl:if>
	
	
</xsl:template>

<xsl:template name="process-directives">
<xsl:param name="thing" />
<xsl:param name="label" />
	
	<xsl:variable name="dates">

		<xsl:choose>
			<xsl:when test="matches(did/unitid[@label='Reference'], '^SxMOA1/3/[0-9]*$')">
				
				<xsl:value-of select="substring-after(normalize-space(did/unittitle), 'Directive: ')" />
	
			</xsl:when>
			<!--
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/3/[0-9]*$')">
				
				<xsl:value-of select="substring-after(normalize-space(../did/unittitle), 'Directive: ')" />
	
			</xsl:when>
			<xsl:when test="matches(../../did/unitid[@label='Reference'], '^SxMOA1/3/[0-9]*$')">
				
				<xsl:value-of select="substring-after(normalize-space(../../did/unittitle), 'Directive: ')" />
				
	
			</xsl:when>
			-->
			<xsl:otherwise />
		</xsl:choose>
	</xsl:variable>
	
	<xsl:if test="not($dates = '')">
		<xsl:comment>SALDA Date = <xsl:value-of select="$dates" /></xsl:comment>
		
		<xsl:call-template name="make-salda-unitdate">
			<xsl:with-param name="thing" select="$thing"/>
			<xsl:with-param name="label" select="$label"/>
			<xsl:with-param name="dates" select="$dates"/>
		</xsl:call-template>
		
		
	</xsl:if>

</xsl:template>

<xsl:template name="process-diaries">
<xsl:param name="thing" />
<xsl:param name="label" />

	<xsl:variable name="dates">
		<xsl:choose>
			<xsl:when test="matches(did/unitid[@label='Reference'], '^SxMOA1/4/[0-9]*$')">
				
				<xsl:value-of select="substring-after(normalize-space(did/unittitle), 'Diary: ')" />
	
			</xsl:when>
			<!--
			<xsl:when test="matches(../did/unitid[@label='Reference'], '^SxMOA1/4/[0-9]*$')">
				
				<xsl:value-of select="substring-after(normalize-space(../did/unittitle), 'Diary: ')" />
				
			</xsl:when>
			<xsl:when test="matches(../../did/unitid[@label='Reference'], '^SxMOA1/4/[0-9]*$')">
				
				<xsl:value-of select="substring-after(normalize-space(../../did/unittitle), 'Diary: ')" />
				
			</xsl:when>
			-->
			<xsl:otherwise />
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="not($dates = '')">
		<xsl:comment>SALDA Date = <xsl:value-of select="$dates" /></xsl:comment>
		
		<xsl:call-template name="make-salda-unitdate">
			<xsl:with-param name="thing" select="$thing"/>
			<xsl:with-param name="label" select="$label"/>
			<xsl:with-param name="dates" select="$dates"/>
		</xsl:call-template>
		
		
	</xsl:if>
	
</xsl:template>

<xsl:template name="process-worktowns">
<xsl:param name="thing" />
<xsl:param name="label" />

</xsl:template>



<xsl:template name="make-salda-unitdate">
<xsl:param name="thing" />
<xsl:param name="label" />
<xsl:param name="dates" />

	<xsl:choose>
		<xsl:when test="contains($dates, '-')">

			<xsl:variable name="startend" select="tokenize($dates, '-')"/>

			<xsl:variable name="start" select="normalize-space($startend[1])"/>
			<xsl:variable name="end" select="normalize-space($startend[2])"/>

			<xsl:comment>SALDA start = <xsl:value-of select="$start" /></xsl:comment>
			<xsl:comment>SALDA end = <xsl:value-of select="$end" /></xsl:comment>
				
			<xsl:choose>
				<xsl:when test="contains($start, ' ')">
					<xsl:variable name="split" select="tokenize($start, ' ')"/>
		
					<xsl:variable name="startmm" select="format-number(index-of($longmonths, lower-case($split[1])), '00')" />
					<xsl:variable name="startyyyy" select="$split[2]"/>
						
					<xsl:variable name="split" select="tokenize($end, ' ')"/>

					<xsl:variable name="endmm" select="format-number(index-of($longmonths, lower-case($split[1])), '00')" />
					<xsl:variable name="endyyyy" select="$split[2]"/>
						
						
					<xsl:comment>SALDA startmm = <xsl:value-of select="$startmm" /></xsl:comment>
					<xsl:comment>SALDA startyyyy = <xsl:value-of select="$startyyyy" /></xsl:comment>
						
					<xsl:comment>SALDA endmm = <xsl:value-of select="$endmm" /></xsl:comment>
					<xsl:comment>SALDA endyyyy = <xsl:value-of select="$endyyyy" /></xsl:comment>
						
					<xsl:element name="dcterms:date">
						<xsl:value-of select="concat($startyyyy, $startmm, '-', $endyyyy, $endmm)" />
					</xsl:element>

					<xsl:call-template name="make-unitdate-event">
						<xsl:with-param name="thing" select="$thing" />
						<xsl:with-param name="label" select="$label" />
						<xsl:with-param name="dates" select="concat($startyyyy, $startmm, '-', $endyyyy, $endmm)" />
					</xsl:call-template>
					
				</xsl:when>
				<xsl:otherwise>
		
					<xsl:variable name="startmm" select="format-number(index-of($longmonths, lower-case($start)), '00')" />
						
					<xsl:variable name="split" select="tokenize($end, ' ')"/>
					<xsl:variable name="endmm" select="format-number(index-of($longmonths, lower-case($split[1])), '00')" />
					<xsl:variable name="endyyyy" select="$split[2]"/>
					<xsl:variable name="startyyyy" select="$split[2]"/>

					<xsl:comment>SALDA startmm = <xsl:value-of select="$startmm" /></xsl:comment>
					<xsl:comment>SALDA startyyyy = <xsl:value-of select="$startyyyy" /></xsl:comment>
						
					<xsl:comment>SALDA endmm = <xsl:value-of select="$endmm" /></xsl:comment>
					<xsl:comment>SALDA endyyyy = <xsl:value-of select="$endyyyy" /></xsl:comment>

					<xsl:element name="dcterms:date">
						<xsl:value-of select="concat($startyyyy, $startmm, '-', $endyyyy, $endmm)" />
					</xsl:element>

					<xsl:call-template name="make-unitdate-event">
						<xsl:with-param name="thing" select="$thing" />
						<xsl:with-param name="label" select="$label" />
						<xsl:with-param name="dates" select="concat($startyyyy, $startmm, '-', $endyyyy, $endmm)" />
					</xsl:call-template>

				</xsl:otherwise>
			</xsl:choose>
				
				
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="split" select="tokenize($dates, ' ')"/>

			<xsl:variable name="MMMM" select="$split[1]"/>
			<xsl:variable name="yyyy" select="$split[2]"/>
		
			<xsl:variable name="mm" select="format-number(index-of($longmonths, lower-case($MMMM)), '00')" />
		
			<xsl:comment>SALDA mm = <xsl:value-of select="$mm" /></xsl:comment>
			<xsl:comment>SALDA yyyy = <xsl:value-of select="$yyyy" /></xsl:comment>

			<xsl:element name="dcterms:date">
				<xsl:call-template name="type-date">
					<xsl:with-param name="date" select="concat($yyyy, $mm)" />
				</xsl:call-template>
			</xsl:element>

			<xsl:call-template name="make-unitdate-event">
				<xsl:with-param name="thing" select="$thing" />
				<xsl:with-param name="label" select="$label" />
				<xsl:with-param name="dates" select="concat($yyyy, $mm)" />
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>

</xsl:template>

</xsl:stylesheet>

